package com.heima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heima.bean.Employee;
import com.heima.common.PageParam;
import com.heima.common.R;
import com.heima.service.EmployeeService;
import com.heima.utils.BaseContext;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RequestMapping("/employee")
@RestController
public class EmployeeController {

    @Autowired
    private BCryptPasswordEncoder bc;

    @Autowired
    private EmployeeService es;

    /**
     * 登录
     *
     * @param employee
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Employee employee, HttpSession session) { //此处的 @RequestBody 是为了把前端传进来的json字符串数据转换成对象数据
        System.out.println("employee = " + employee);
        Employee loginEmployee = es.login(employee);

        if (loginEmployee != null) {
            //登录成功

            //把员工对象保存到session作用域
            session.setAttribute("employee", loginEmployee);

            //返回查询到的该员工的信息给页面
            return R.success(loginEmployee);
        } else {
            //登录失败！
            return R.error("用户名或密码错误！");
        }
    }

    /**
     * 退出登录
     *
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public R logout(HttpSession session) {
        try {
            //把登录成功的时候，保存的员工数据给删除掉
            //session.removeAttribute("employee");

            //直接强制退出会话
            session.invalidate();
            return R.success("退出成功！！");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("退出失败！！");
        }
    }

    /**
     * 添加员工
     *
     * @param employee
     * @param session
     * @return
     */
    @PostMapping
    public R add(@RequestBody Employee employee, HttpSession session) {
        System.out.println("employee = " + employee);

        //0. 补充数据:: 从页面提交过来的数据不完整，缺少： 密码、创建时间、更新时间、创建用户、更新用户
        String pwd = bc.encode("123456");
        Employee e = (Employee) session.getAttribute("employee");
        employee.setPassword(pwd);

        /*
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        //谁执行了登录，谁就执行了添加操作。
        employee.setCreateUser(e.getId());
        employee.setUpdateUser(e.getId());
        */

        //1. 调用service
        int row = es.add(employee);
        if (row > 0) {
            return R.success("添加员工成功！");
        }
        return R.error("添加员工失败！");
    }

    /**
     * 分页查询
     *
     * @param pageParam
     * @param session
     * @return
     */
    @GetMapping("page")
    public R page(PageParam pageParam, HttpSession session) {
        System.out.println("pageParam = " + pageParam);
        IPage<Employee> employeeIPage = es.page(pageParam);
        System.out.println("employeeIPage = " + employeeIPage.getRecords());
        return R.success(employeeIPage);
    }

    /**
     * 编辑员工
     *
     * @param employee
     * @param session
     * @return
     */
    @PutMapping
    public R update(@RequestBody Employee employee, HttpSession session) {
        //0. 由于执行的是更新操作，所以要修改update_user 和 update_time 这里两个属性的值
        //Employee e = (Employee) session.getAttribute("employee");

        //employee.setUpdateUser(e.getId());
        //employee.setUpdateTime(LocalDateTime.now());

        int row = es.update(employee);
        if (row > 0) {
            return R.success("更新成功！");
        }
        return R.error("更新失败！");
    }
}
